package com.mxl.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mxl.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class User {

    @Autowired
    private IUserService iUserService;

    @GetMapping("/getUser")
    public String getUser(
            @RequestParam("name")  String name
    ){
        System.out.println(name);
        return name;
    }

    // 测试sentinel
    @GetMapping(value = "/hello/{name}")
    public String apiHello(
            @PathVariable("name") String name) {
        return iUserService.sayHello(name);
    }

    // 测试sentinel
    //http://localhost:8090/user/user/api/user/selectUserList?pageNo=1&pageSize=3&name=%E5%B0%8F%E9%BE%99
    //http://localhost:8003/api/user/user/selectUserList?pageNo=1&pageSize=3&name=%E5%B0%8F%E9%BE%99
    @GetMapping(value = "/selectUserList")
    public JSONObject selectUserList(
            @RequestParam(value = "name",defaultValue = "",required = false) String name,
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam("pageSize") Integer pageSize
    ) {
        IPage<Map<String,Object>> ipage =  iUserService.selectUserList(pageNo,pageSize,name);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("date",ipage.getRecords()); //总数据
        jsonObject.put("total",(int) Math.ceil(ipage.getTotal() / (double)pageSize));  //总页数
        jsonObject.put("count",ipage.getTotal());  //总条数
        return  jsonObject;

    }

}
